<template>
  <div class="app-container">
     <div class="filter-container">
      <el-input v-model="listQuery.oid" placeholder="订单号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      
      <el-select v-model="listQuery.status" placeholder="支付状态" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />
      </el-select>
     
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
       
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="订单号"  align="center" >
        <template slot-scope="{row}">
          <span>  {{ row.oid }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="用户名称" align="center"> 
       <template slot-scope="{row}">
           <el-image
                style="width: 30px; height: 30px"
                :src="row.slUserByUid.avatarUrl"
                fit="contain"
            ></el-image>
            <span>  {{ row.slUserByUid.nickname }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="订单类型" min-width="80px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.type == 1">家人充值</span>
           <span v-if="row.type == 2">认证费用</span>
            <span v-if="row.type == 3">诚意金</span>
             <span v-if="row.type == 4">商家充值</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80px" align="center">
        <template slot-scope="{row}">
            <el-tag class="ml-2" type="success" v-if="row.status == 1">支付成功</el-tag>
            <el-tag class="ml-2" type="success" v-else-if="row.status == 0">待支付</el-tag>
            <el-tag class="ml-2" type="info" v-else>支付失败</el-tag>
        </template>
      </el-table-column>
        <el-table-column label="金额" width="80px" align="center">
        <template slot-scope="{row}">
            <span>{{row.totalPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属项目"  align="center">
        <template slot-scope="{row}">
          <span v-if="row.slProjectByPid != ''">{{ row.slProjectByPid.title }}</span>
          <span v-else>平台</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
                 <el-button type="primary" size="mini" @click="handleUpdate(row)">
                  编辑
                </el-button>
          <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="取消"
              icon-color="red"
              title="你是否确定删除此数据?"
                @onConfirm="handleDelete(row,$index)"
            >
              <template #reference>
                <el-button size="mini" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
      
        </template>
      </el-table-column> -->
    </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { GQL } from '@/api/article'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive

const calendarTypeOptions = [
  { key: '1', display_name: '支付成功' },
  { key: '0', display_name: '待支付' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',

  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
         calendarTypeOptions,
      listQuery: {
        page: 1,
        limit: 20,
        oid: undefined,
        status: undefined
      }
    }
  },
  created() {
     this.getList()
  },
  methods: {
  
    getList() {
      this.listLoading = true
      let where = {}
        if(this.listQuery.page != 1){
             where = {"filter": {"data": { "notEqualTo": "" }}, "first": this.listQuery.limit, "offset": this.listQuery.page*10-10, "orderBy": "CREATETIME_DESC"}
        }else{
         where = {"filter": {"data": { "notEqualTo": "" }}, "first": this.listQuery.limit, "offset": this.listQuery.page-1, "orderBy": "CREATETIME_DESC"}
        }
        if(this.listQuery.oid != undefined ){
            where.filter.oid = {"like":"%"+this.listQuery.oid+"%"}
        }
        if(this.listQuery.status != undefined ){
            where.filter.status = {"equalTo": this.listQuery.status}
        }
        const query = {
               "operationName": "allSlUserOrders",
                "query": "query allSlUserOrders($filter: SlUserOrderFilter, $offset: Int, $first: Int, $orderBy: [SlUserOrdersOrderBy!] = [PRIMARY_KEY_ASC]) { allSlUserOrders(filter:$filter first: $first offset: $offset orderBy: $orderBy ) { nodes { uid type totalPrice status pid payTime orderId oid data slUserByUid { nickname avatarUrl } slProjectByPid { pid title } } } }",
                "variables":where
        }
       GQL(query).then(response => {
        this.list = response.data.allSlUserOrders.nodes
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
         title:'',
            img:'',
            solt:'',
            url:'',
            status:'',
      }
    },
   
    handleDelete(row, index) {
       
          let query = {
            "operationName": "deleteSlSystemBannerById",
            "query": "mutation deleteSlSystemBannerById($input:DeleteSlSystemBannerByIdInput!) { deleteSlSystemBannerById(input:$input) { deletedSlSystemBannerId } }",
            "variables": {input: {id: row.id}}
          }
          GQL(query).then((res) => {
               if(res.errors){
                  this.$notify({
                    title: '删除失败',
                    message: '删除项目失败',
                    type: 'error',
                    duration: 2000
                  })
              }else{
                  this.$notify({
                    title: '删除成功',
                    message: '删除项目成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.list.splice(index, 1)
              }
      
          })
        
    }
  }
}
</script>
<style scoped>
.demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 350px;
    height: 180px;
    display: block;
  }
</style>
